//******************************************
//	TopCMM ActionScript 2.0 Class Library
//	Copyright (c) 2006 TopCMM SOFTWARE
//******************************************
/** 
 * @author		Tim
 * @version		123flashchat 7.0, ActionScript 2.0, FlashPlayer 8.0
 */
class topcmm.datas.ObjectParser {

	public static function object2String(arg):String {
		var c, i, l, s = '', v;
		switch (typeof arg) {
			case 'object' :			
			if (arg) {
				if (arg instanceof Array) {
						for (i = 0; i < arg.length; ++i) {
							v = object2String(arg[i]);
						if (s) {
								s += ',';
						}
							s += v;
					}
						return '[' + s + ']';
				} else if (typeof arg.toString != 'undefined') {
						for (i in arg) {
							v = arg[i];
						if (typeof v != 'undefined' && typeof v != 'function') {
								v = object2String(v);
							if (s) {
									s += ',';
							}
								s += object2String(i) + ':' + v;
						}
					}
						return '{' + s + '}';
				}
			}
				return 'null';
			case 'number' :
				return isFinite(arg) ? String(arg):'null';
			case 'string' :
				l = arg.length;
				s = '"';
				for (i = 0; i < l; i += 1) {
					c = arg.charAt(i);
				if (c >= ' ') {
					if (c == '\\' || c == '"') {
							s += '\\';
					}
						s += c;
				} else {
						switch (c) {
							case '\b' :
								s += '\\b';
								break;
							case '\f' :
								s += '\\f';
								break;
							case '\n' :
								s += '\\n';
								break;
							case '\r' :
								s += '\\r';
								break;
							case '\t' :
								s += '\\t';
								break;
							default :
								c = c.charCodeAt();
								s += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
					}
				}
			}
				return s + '"';
			case 'boolean' :
				return String(arg);
			default :
				return 'null';
		}
	}

	public static function string2Object(text:String):Object {
		var at = 0;
		var ch = ' ';
		var _value:Function;
		var _error:Function = function (m) {
			throw {name:'JSONError', message:m, at:at - 1, text:text};
		};
		var _next:Function = function () {
			ch = text.charAt(at);
			at += 1;
			return ch;
		};
		var _white:Function = function () {
			while (ch) {
				if (ch <= ' ') {
					_next();
				} else if (ch == '/') {
					switch (_next()) {
						case '/' :
						while (_next() && ch != '\n' && ch != '\r') {
						}
							break;
						case '*' :
							_next();
							for (; ; ) {
							if (ch) {
								if (ch == '*') {
									if (_next() == '/') {
											_next();
											break;
									}
								} else {
										_next();
								}
							} else {
									_error("Unterminated comment");
							}
						}
							break;
						default :
							_error("Syntax error");
					}
				} else {
					break;
				}
			}
		};
		var _string:Function = function () {
			var i, s = '', t, u;
			var outer:Boolean = false;
			if (ch == '"') {
				while (_next()) {
					if (ch == '"') {
						_next();
						return s;
					} else if (ch == '\\') {
						switch (_next()) {
							case 'b' :
								s += '\b';
								break;
							case 'f' :
								s += '\f';
								break;
							case 'n' :
								s += '\n';
								break;
							case 'r' :
								s += '\r';
								break;
							case 't' :
								s += '\t';
								break;
							case 'u' :
								u = 0;
								for (i = 0; i < 4; i += 1) {
									t = parseInt(_next(), 16);
								if (!isFinite(t)) {
										outer = true;
										break;
								}
									u = u * 16 + t;
							}
							if (outer) {
									outer = false;
									break;
							}
								s += String.fromCharCode(u);
								break;
							default :
								s += ch;
						}
					} else {
						s += ch;
					}
				}
			}
			_error("Bad string");
		};
		var _array:Function = function () {
			var a = [];
			if (ch == '[') {
				_next();
				_white();
				if (ch == ']') {
					_next();
					return a;
				}
				while (ch) {
					a.push(_value());
					_white();
					if (ch == ']') {
						_next();
						return a;
					} else if (ch != ',') {
						break;
					}
					_next();
					_white();
				}
			}
			_error("Bad array");
		};
		var _object:Function = function () {
			var k, o = {};
			if (ch == '{') {
				_next();
				_white();
				if (ch == '}') {
					_next();
					return o;
				}
				while (ch) {
					k = _string();
					_white();
					if (ch != ':') {
						break;
					}
					_next();
					o[k] = _value();
					_white();
					if (ch == '}') {
						_next();
						return o;
					} else if (ch != ',') {
						break;
					}
					_next();
					_white();
				}
			}
			_error("Bad object");
		};
		var _number:Function = function () {
			var n = '', v;
			if (ch == '-') {
				n = '-';
				_next();
			}
			while (ch >= '0' && ch <= '9') {
				n += ch;
				_next();
			}
			if (ch == '.') {
				n += '.';
				while (_next() && ch >= '0' && ch <= '9') {
					n += ch;
				}
			}
			//v = +n;
			v = 1 * n;
			if (!isFinite(v)) {
				_error("Bad number");
			} else {
				return v;
			}
		};
		var _word:Function = function () {
			switch (ch) {
				case 't' :
				if (_next() == 'r' && _next() == 'u' && _next() == 'e') {
						_next();
						return true;
				}
					break;
				case 'f' :
				if (_next() == 'a' && _next() == 'l' && _next() == 's' && _next() == 'e') {
						_next();
						return false;
				}
					break;
				case 'n' :
				if (_next() == 'u' && _next() == 'l' && _next() == 'l') {
						_next();
						return null;
				}
					break;
			}
			_error("Syntax error");
		};
		_value = function () {
			_white();
			switch (ch) {
				case '{' :
					return _object();
				case '[' :
					return _array();
				case '"' :
					return _string();
				case '-' :
					return _number();
				default :
					return ch >= '0' && ch <= '9' ? _number():_word();
			}
		};
		return _value();
	}
}
